package com.systemmanger.controller;

import com.bean.WmsTGroup;
import com.bean.WmsTPermission;
import com.bean.WmsTUG;
import com.bean.WmsTUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.systemmanger.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @AUTHOR(闫晨航)
 * @date2021-12-06 15:53
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    //用户管理页面
    @RequestMapping("xitong_user.do")
    public String getAllUsers(HttpServletRequest request){
        int pageNum = 1;
        int pageRows = 15;
        PageHelper.startPage(pageNum,pageRows);
        List<WmsTUser> wmsTUsers = userService.getAllUsers();
        PageInfo<WmsTUser> pageInfo = new PageInfo<>(wmsTUsers);
        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(10);
        list.add(15);
        request.getSession().setAttribute("pageRows",pageRows);
        request.getSession().setAttribute("list",list);
        System.out.println(pageInfo);
        if(pageInfo!=null){
            request.getSession().setAttribute("pageInfo",pageInfo);
        }
        return "xitong/xitong_user";
    }
    //用户页面分页
    @RequestMapping("xitong_selectUser.do")
    public String selectUser(int pageRows, String pageNum,String userID,String userName,HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        request.getSession().removeAttribute("userID");
        request.getSession().removeAttribute("userName");
        int pageNum1 = 1;
        if(userName!=null){
            if(!userName.equals("")){
//                custName = new String(custName.getBytes("ISO-8859-1"),"utf-8");
                System.out.println(userName);
                map.put("userName",userName);
                request.getSession().setAttribute("userName",userName);
            }
        }
        if(userID!=null){
            if(!userID.equals("")){
//                custName = new String(custName.getBytes("ISO-8859-1"),"utf-8");
                System.out.println(userID);
                map.put("userID",userID);
                request.getSession().setAttribute("userID",userID);
            }
        }
        if(pageNum!=null){
            pageNum1=Integer.parseInt(pageNum);
        }
        request.getSession().setAttribute("pageRows",pageRows);
        PageHelper.startPage(pageNum1,pageRows);
        List<WmsTUser> wmsTUsers = userService.getUsers(map);
        PageInfo<WmsTUser> pageInfo = new PageInfo<>(wmsTUsers);

        if(pageInfo!=null){
            System.out.println(pageInfo);
            request.getSession().setAttribute("pageInfo",pageInfo);
        }
        return "xitong/xitong_user";
    }
    //根据id获取一个user
    @RequestMapping("xitong_getOneuser.do")
    @ResponseBody
    public String getOneUser(String userID,HttpServletRequest request){
        List<WmsTPermission> wmsTPermissions = userService.getAllPerm();
        WmsTPermission wmsTPermission = userService.getOnePermByUid(userID);
        WmsTUser wmsTUser =  userService.getuserByUserId(userID);
        request.getSession().setAttribute("wmsTPermission",wmsTPermission);
        request.getSession().setAttribute("wmsTPermissions",wmsTPermissions);
        request.getSession().setAttribute("wmsTUser",wmsTUser);
        return "111";
    }
    //修改
    @RequestMapping("xitong_updateUser.do")
    public void updateUser(WmsTUser wmsTUser, String edit, HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println(wmsTUser);
//        System.out.println(edit);
        if(edit.equals("delete")){
            userService.delByUserId(wmsTUser.getUserId());
        }else if(edit.equals("edit")){
            WmsTPermission wmsTPermission = userService.getOnePermByPid(wmsTUser.getUserType());
            wmsTUser.setUserType(wmsTPermission.getPermName());
            WmsTGroup wmsTGroup = userService.getOgroup(wmsTPermission.getPermId());
            userService.updateugByGroupId(wmsTGroup.getGroupId(),wmsTUser.getUserId());
            userService.updateByUser(wmsTUser);
        }
        request.getSession().removeAttribute("wmsTUser");
        response.sendRedirect("xitong_user.do");
    }
    @RequestMapping("getAllgroup.do")
    @ResponseBody
    public List<WmsTPermission> getAllgroup(){
        List<WmsTPermission> wmsTPermissions = userService.getAllPerm();
//        System.out.println(wmsTPermissions);
        return wmsTPermissions;
    }
    @RequestMapping("updateGroup.do")
    @ResponseBody
    public void updateGroup(String permId,HttpServletRequest request){
        System.out.println(permId);
        WmsTPermission wmsTPermission = userService.getOnePermByPid(permId);
        System.out.println("256"+wmsTPermission);
        request.getSession().setAttribute("wmsTPermission",wmsTPermission);
    }

}
